/**
 * 登录
 */


$(function () {
    //生成动态验证码
    // picCode = drawPic();
    // console.log(picCode);
    layui.use(['form', 'layer'], function () {
        var form = layui.form, layer = layui.layer;

        //错误提示jq object
        const $errorTip = $('.error-tip ');

        /**
         * 登录
         * @param field
         */
        let login = function(field) {
            let flag = true;

            //校验参数
            if(ValidateUtils.checkUserName(field.user_number) != 'ok'){
                layer.tips('用户名格式错误', $('#loginForm>.layui-form-item input[name=user_number]'), {
                    tips: [1, '#cc2039']
                });
                flag = false;
            }
            if(ValidateUtils.checkSimplePassword(field.password) != 'ok'){
                layer.tips('密码格式错误', $('#loginForm>.layui-form-item input[name=password]'), {
                    tips: [1, '#cc2039']
                });
                flag = false;
            }
            if(!flag){return;}

            //显示loading动画
            // $(CONTENT_LOADING).insertBefore()
            $('#loginButton').addClass('layui-hide');
            $('#loading-icon').removeClass('layui-hide');

            let success = false;
            $.ajax({
                url:'/user/login',
                type: 'POST',
                data:field,
                dataType: 'json',
                success: (res)=>{
                    if(res.code == "1000"){
                        $('<label style="display: block;">登录成功，正在跳转...</label>').insertAfter($('#loading-icon'));
                        success = true;
                        window.location.href = '/user/index';
                    }else{
                        $errorTip.removeClass('layui-hide').text(res.message);
                    }

                },
                error: (res)=>{
                    layer.msg((res.status || '') + '-出错啦', {icon:2});
                },
                complete: ()=>{
                    if(success){return;}
                    $('#loading-icon').addClass('layui-hide');
                    $('#loginButton').removeClass('layui-hide');
                }
            });
        };


        /**
         * 表单提交
         */
        form.on("submit(loginButton)", function (data) {
            $(data.elem).addClass('layui-disable');
            console.log(data.field);
            login(data.field);
            return false;
        });

        $('#loginForm input[name]').on('focus', ()=>{
            $errorTip.addClass('layui-hide');
        })

    })
});

/**
 * 还没实现手机验证码
 * @type {number}
 */
//定时发送验证码
/*

var wait = 60;
var startJob;

//o 对象
function send(o, flag) {
    if (!flag) {
        return false;
    }
    //第一次秒数
    if (wait == 60) {
        o.setAttribute("disabled", true);
        //自定义验证规则
        $.post("/user/sendMsg", $("#loginForm").serialize(), function (data) {
            console.log("data:" + data);
            if (data.code == "1000") {
                layer.msg("发送短信成功");
            } else {
                $("#password").val("");
                picCode = drawPic();
                $("#code").val("");
                //禁用发送短信验证码按钮
                o.removeAttribute("disabled");
                //o.value = "获取验证码";
                wait = 60;
                flag = false;
                layer.alert(data.message);
            }
            return false;
        });
    }
    if (wait == 0) {
        o.removeAttribute("disabled");
        $("#msgBtn").html("获取验证码");
        wait = 60;
    } else {
        o.setAttribute("disabled", true);
        if (wait < 60) {
            $("#msgBtn").html("<span style='margin-left: -12px;'>" + wait
                + "s后可重新发送</span>");
        }
        wait--;
        startJob = setTimeout(function () {
            if (wait == 0) {
                flag = true
            }
            ;
            send(o, flag)
        }, 1000)
    }
}

function closeSend() {
    $("#msgBtn").removeAttr("disabled");
    $("#msgBtn").html("获取验证码");
    clearTimeout(startJob);
}

function sendMsg() {
    var flag = checkParams();
    if (flag != false) {

        //自定义验证规则
        $.post("/user/sendMsg", $("#loginForm").serialize(), function (data) {
            console.log("data:" + data)
            if (data.code == "1000") {
                layer.msg("发送短信成功");
            } else {
                //$("#password").val("");
                picCode = drawPic();
                $("#code").val("");
                layer.alert(data.message);
            }
        });
    }
}



function checkParams() {
    //  校验
    var username = $("#username").val();
    var password = $("#password").val();
    var code = $("#code").val();

    if ("ok" != ValidateUtils.checkUserName(username) || "ok"
        != ValidateUtils.checkSimplePassword(password)) {
        // layer.alert("请您输入正确的用户名和密码");
        alert("格式错误，请您输入正确的用户名和密码");
        $("#password").val("");
        picCode = drawPic();
        $("#code").val("");

        return false;
    }
    // if ("ok" != ValidateUtils.checkPicCode(code)) {
    //     //tips层-右
    //     layer.tips(ValidateUtils.checkPicCode(code), '#canvas', {
    //         tips: [2, '#78BA32'], //还可配置颜色
    //         tipsMore: true
    //     });
    //     return false;
    // }
    // //输入的验证码和显示的验证码不一致
    // if (picCode.toLowerCase() != code.toLowerCase()) {
    //     //tips层-右
    //     layer.tips("请您输入正确的验证码", '#canvas', {
    //         tips: [2, '#78BA32'], //还可配置颜色
    //         tipsMore: true
    //     });
    //     return false;
    // }

    return true;
}
*/